
package com.gp.activity.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.gp.R;
import com.gp.activity.base.BaseActivity;
import com.gp.activity.detail.SelfGuideDetailActivity;
import com.gp.adapter.MenuGridViewAdapter;
import com.gp.adapter.SelfGuideGridViewAdapter;
import com.gp.data.Sights;
import com.gp.model.SelfGuideListItem;

public class SelfGuideActivity extends BaseActivity {

    // private ListView mSelfGuideListView;
    private GridView mGridView;
    // 返回按钮
    private Button mButton;
    private List<SelfGuideListItem> mGuideListItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selfguide);
        intiView();
        initValue();
        bindData();
        initListener();
    }

    /**
     * @Title: intiView
     * @param
     * @return void
     * @throws
     * @date Jul 18, 2013 2:59:45 PM
     * @version 1.0
     * @Description: 初始化控件
     */
    private void intiView() {

        // mSelfGuideListView = (ListView) findViewById(R.id.lv_selfguide);
        mButton = (Button) findViewById(R.id.btn_selfguide_back);
        mGridView = (GridView) findViewById(R.id.gv_selfguide_gridview);
    }

    /**
     * @Title: bindData
     * @param
     * @return void
     * @throws
     * @date Jul 18, 2013 3:01:23 PM
     * @version 1.0
     * @Description: 为控件绑定数据
     */
    private void bindData() {

        mGridView.setAdapter(new SelfGuideGridViewAdapter(this, mGuideListItems));

    }

    private void initValue() {

        mGuideListItems = new ArrayList<SelfGuideListItem>();

        for (int i = 0; i < Sights.imaID.length; i++) {

            SelfGuideListItem qGuideListItem = new SelfGuideListItem(
                    Sights.imaID[i], Sights.name[i], Sights.type[i],
                    Sights.level[i], Sights.price[i], Sights.opentime[i],
                    Sights.phone[i],Sights.des[i]);

            mGuideListItems.add(qGuideListItem);
        }

    }

    
    private String readFileFromRaw(int pResID) {

        // 资源句柄
        Resources qResources = this.getResources();
        // 资源输入流
        InputStream qInputStream = null;
        // String缓冲器，缓存从res/raw/文件夹读出的数据
        StringBuffer qStringBuffer = new StringBuffer();
        // 用于读取缓存器中的数据
        BufferedReader qBufferedReader = null;
        String qString;
        //
        qStringBuffer.append("");

        try {
            // 从res/raw/文件夹独处数据
            qInputStream = qResources.openRawResource(pResID);
            // 获取数据，编码格式UTF-8，支持中文
            qBufferedReader = new BufferedReader(new InputStreamReader(
                    qInputStream, "UTF-8"));
            while ((qString = qBufferedReader.readLine()) != null) {

                qStringBuffer.append(qString);
                qStringBuffer.append("\n");
            }
        }
        catch (NotFoundException e) {
            Toast.makeText(this, "文本文件不存在", 100).show();
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e) {
            Toast.makeText(this, "文本编码出现异常", 100).show();
            e.printStackTrace();
        }
        catch (IOException e) {
            Toast.makeText(this, "文件读取错误", 100).show();
            e.printStackTrace();
        }
        finally {
            try {
                if (qInputStream != null) {
                    qInputStream.close();
                }
                if (qBufferedReader != null) {
                    qBufferedReader.close();
                }
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return qStringBuffer.toString();
    }
    /**
     * @Title: initListener
     * @param
     * @return void
     * @throws
     * @date Jul 18, 2013 3:04:04 PM
     * @version 1.0
     * @Description: 初始化控件监听器
     */
    private void initListener() {

        // 返回按钮的监听
        mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                SelfGuideActivity.this.finish();
            }
        });

        // 列表项点击监听
        mGridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                    int position, long id) {

                Intent qIntent = new Intent();

                qIntent.setClass(SelfGuideActivity.this,
                        SelfGuideDetailActivity.class);
                // 将数据根据点击位置传到SelfGuideDetailActivity，这里的数据放在Sights这个类里面
                qIntent.putExtra("imaID", mGuideListItems.get(position).getImaId());
                qIntent.putExtra("name", mGuideListItems.get(position).getName());
                qIntent.putExtra("type", mGuideListItems.get(position).getType());
                qIntent.putExtra("level", mGuideListItems.get(position).getLevel());
                qIntent.putExtra("price", mGuideListItems.get(position).getPrice());
                qIntent.putExtra("opentime", mGuideListItems.get(position).getOpentime());
                qIntent.putExtra("phone", mGuideListItems.get(position).getphone());
                qIntent.putExtra("des", readFileFromRaw(mGuideListItems.get(position).getDes()));
                qIntent.putExtra("location",position);
                

                SelfGuideActivity.this.startActivity(qIntent);
            }
        });

    }
}
